﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using HotShot.Service.Entities;
using HotShot.Entities.Messages;

namespace HotShot.Service.BusinessLogic.Worker
{
    public class HealthCommunicationWorker : WorkerBase
    {
        protected override Guid ProcessorId
        {
            get { return Guid.NewGuid(); }
        }

        public override void Initialize(string executorMachineName, string processorName, int periodicityMilliSec)
        {
            int pms;
            if(Int32.TryParse(ConfigurationManager.AppSettings["HealthWatchdog.TimeMs"], out pms)) {
                periodicityMilliSec = pms;
            }
            base.Initialize(executorMachineName, processorName, periodicityMilliSec);
        }

        protected override bool Work()
        {
            QueueCommunicationLogic.Current.Broadcast(Messages.Ping(null), null);

            long timeout = DateTime.UtcNow.AddMilliseconds(-3*PeriodicityMilliSec).Ticks;
            ClusterProcessorLogic.Current.GetKnownNodes(true, false).ForEach(
                n => n.IsActive = n.Ticks >= timeout);

            return false;
        }
    }
}
